package org.gtugs.omsk.funnycats.scenes.classes;

import org.gtugs.omsk.funnycats.engine.beans.Position;
import org.gtugs.omsk.funnycats.engine.beans.Size;

public class FunnyConverter {

	public static FunnyConverter scaleInCenter(Size worldSize, Size displaySize){
		final float scale = Math.min(displaySize.width / worldSize.width, displaySize.height / worldSize.height);
		return new FunnyConverter(worldSize, scale, Position.make(displaySize.width / 2.0f, displaySize.height / 2.0f));
	}
	
	public Position positonDisplayToWorld(Position p){
		final float x = (p.x - center.x) / scale + worldSize.width / 2.0f;
		final float y = (p.y - center.y) / scale + worldSize.height / 2.0f;
		
		return Position.make(x, y);
	}
	
	public Position positionWorldToDisplay(Position p){
		final float x = center.x - scale * ( worldSize.width / 2.0f - p.x);
		final float y = center.y - scale * ( worldSize.height / 2.0f - p.y);
		
		return Position.make(x, y);
	}
	
	public float lengthDisplayToWorld(float length){
		final Position dp1 = Position.make(0.0f, 0.0f);
		final Position wp1 = positonDisplayToWorld(dp1);
		final Position dp2 = Position.make(length, 0.0f);
		final Position wp2 = positonDisplayToWorld(dp2);
		return Math.abs(wp2.x - wp1.x);
	}
	
	public float lengthWorldToDisplay(float length){
		final Position wp1 = Position.make(0.0f, 0.0f);
		final Position dp1 = positionWorldToDisplay(wp1);
		final Position wp2 = Position.make(length, 0.0f);
		final Position dp2 = positionWorldToDisplay(wp2);
		return Math.abs(dp2.x - dp1.x);
	}
	
	public Size sizeDisplayToWorld(Size size){
		final Size worldSize = Size.make(lengthDisplayToWorld(size.width), lengthDisplayToWorld(size.height));
		return worldSize;
	}
	
	public Size sizeWorldToDisplay(Size size){
		final Size displaySize = Size.make(lengthWorldToDisplay(size.width), lengthWorldToDisplay(size.height));
		return displaySize;
	}
	
	private FunnyConverter(Size worldSize, float scale, Position center) {
		this.worldSize = worldSize;
		this.scale = scale;
		this.center = center;
	}
	
	private final Size worldSize;
	private final float scale;
	private final Position center;
}
